package com.ciwong.epaper.widget;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.ciwong.epaper.R;

/**
 * 播放状态控件
 * Created by lzr on 2017/4/13.
 * 句子
 */
public class PlayStateDubView extends LinearLayout {


    private RoundProgressBar round_progress_bar;
    private ImageView playing;
    private ImageView recording;

    private final int TIME_INTERVAL = 100;
    /**
     * 1 为播放状态  2为录音状态
     */
    private int isPlayButStatus = PLAY_STATUE;
    public static final int PLAY_STATUE = 1;
    public static final int RECORD_STATUE = 2;
    /**
     * ture 为播放状态  false为录音状态
     */
    private boolean isPlay = false;

    private Long timeCount;

    private int progress;

    private boolean isHandler = false;

    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    if (isPlay) {
                        progress++;
                        round_progress_bar.setProgress(progress);
                        mHandler.sendEmptyMessageDelayed(0, TIME_INTERVAL);
                        isHandler = true;
                    } else {
                        mHandler.removeMessages(0);
                        isHandler = false;
                    }

                    break;
            }
        }
    };

    public PlayStateDubView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public PlayStateDubView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }


    private void initView() {
        setOrientation(LinearLayout.VERTICAL);
        setBackgroundColor(getResources().getColor(R.color.white));
        View.inflate(getContext(), R.layout.play_state_dub_view, this);
        round_progress_bar = (RoundProgressBar) findViewById(R.id.round_progress_bar);
        playing = (ImageView) findViewById(R.id.problemPlay);
        recording = (ImageView) findViewById(R.id.img_micro_recording);

    }


    public void setTimeCount(Long timeCount) {
        this.timeCount = timeCount;
        int max = timeCount.intValue() / TIME_INTERVAL;
        round_progress_bar.setMax(max);

    }

    public void setTimeAndPlayBtnState(Long timeCount, int isPlayButStatus) {
        this.timeCount = timeCount;
        int max = timeCount.intValue() / TIME_INTERVAL;
        round_progress_bar.setMax(max);
        round_progress_bar.setProgress(0);
        this.isPlayButStatus = isPlayButStatus;
        if (isPlayButStatus == PLAY_STATUE) {
            playing.setVisibility(View.VISIBLE);
            recording.setVisibility(GONE);
        } else if (isPlayButStatus == RECORD_STATUE) {
            playing.setVisibility(View.GONE);
            recording.setVisibility(VISIBLE);
        } else {
            playing.setVisibility(View.GONE);
            recording.setVisibility(GONE);
        }
    }

    public void updatePlayProgress(long total, long progress) {
//        round_progress_bar.setVisibility(View.VISIBLE);
//        round_progress_bar.setMax(total);
//        round_progress_bar.setProgress(progress);
    }

    /**
     * 录音
     */
    public void startRecord() {
        setVisibility(VISIBLE);
        setPlayBtnState(false, View.GONE);
        setRecordState(true, View.GONE);
        isPlay = true;
        round_progress_bar.setProgress(0);
        round_progress_bar.setVisibility(VISIBLE);
        progress = 0;
        if (!isHandler) {
            mHandler.sendEmptyMessageDelayed(0, TIME_INTERVAL);
            isHandler = true;
        }
    }

    /**
     * 当独录音
     */
    public void startRecordHandler() {
        setVisibility(VISIBLE);
        setPlayBtnState(false, View.GONE);
        setRecordState(true, View.GONE);
        isPlay = true;
        round_progress_bar.setProgress(0);
        round_progress_bar.setVisibility(VISIBLE);
        progress = 0;
        mHandler.sendEmptyMessageDelayed(0, TIME_INTERVAL);
        isHandler = true;
    }

    /**
     * 播放
     */
    public void startPlay() {
        setVisibility(VISIBLE);
        setPlayBtnState(true, View.GONE);
        setRecordState(false, View.GONE);
        isPlay = true;
        round_progress_bar.setProgress(0);
        round_progress_bar.setVisibility(VISIBLE);
        progress = 0;
        mHandler.sendEmptyMessageDelayed(0, TIME_INTERVAL);
        isHandler = true;
    }

    /**
     * 设置播放图标的显示状态
     *
     * @param isShow
     */
    public void setPlayBtnState(boolean isShow, int showView) {
        if (playing == null) {
            return;
        }
        if (isShow) {
            playing.setVisibility(View.VISIBLE);
//            Drawable drawable = playing.getDrawable();
//            if (drawable != null && drawable instanceof AnimationDrawable) {
//                ((AnimationDrawable) drawable).start();
//            }

        } else {
            playing.setVisibility(showView);
//            Drawable drawable = playing.getDrawable();
//            if (drawable != null && drawable instanceof AnimationDrawable) {
//                ((AnimationDrawable) drawable).stop();
//            }
        }
    }

    //设置录音图标的显示状态
    public void setRecordState(boolean isRecording, int showView) {
        if (null == recording) {
            return;
        }
        if (isRecording) {
            recording.setVisibility(VISIBLE);
            Drawable drawable = recording.getDrawable();
            if (drawable != null && drawable instanceof AnimationDrawable) {
                ((AnimationDrawable) drawable).start();
            }
        } else {
            recording.setVisibility(showView);
            Drawable drawable = recording.getDrawable();
            if (drawable != null && drawable instanceof AnimationDrawable) {
                ((AnimationDrawable) drawable).stop();
            }
        }
    }

    public boolean isPlay() {
        return isPlay;
    }

    public void closeView() {
        stopPlay();
    }
//

    /**
     * 暂停并隐藏
     */
    private void stopPlay() {
        setPlayBtnState(false, View.GONE);
        setRecordState(false, View.GONE);
        setVisibility(INVISIBLE);
        isPlay = false;
        round_progress_bar.setVisibility(View.GONE);
        mHandler.removeMessages(0);
        isHandler = false;
//        index=0;
//        cro=0;
//        startPlay=false;
//        invalidate();
    }

    /**
     * 暂停播放
     */
    public void stopOnlyPlay() {
        setRecordState(false, View.GONE);
        setPlayBtnState(false, View.VISIBLE);
        isPlay = false;
//        round_progress_bar.setProgress(round_progress_bar.getMax());
        round_progress_bar.setProgress(0);
        mHandler.removeMessages(0);
        isHandler = false;
    }

    /**
     * 暂停录音
     */
    public void stopOnlyRecord() {
        setPlayBtnState(false, View.GONE);
        setRecordState(false, View.VISIBLE);
        round_progress_bar.setProgress(round_progress_bar.getMax());
        mHandler.removeMessages(0);
        isHandler = false;
    }
}
